The python Language Reference

CPython -> Python implmentation in C
Python program is read by a parser, input to the parser is a stream of tokens, generated by lexical analyzer.

  1. Logical Lines -> The end of a logical line is represented by NEWLINE
  2. Physical Lines -> It is a sequence of characters terminated by an end-of-line sequence
  3. Comments -> It starts with # </ol> A comment in the first or second line of the form coding[=:]\s*([-\w.]+) is processed as a encoding declaration.
  4. Two or more physical lines can be interpretted as logical lines by using backslash().
    Lines in parenthesis can be split without backslahes

    
    
    In [4]:
    def \
    quicksort():
        pass
    

    Everything is represented by objects in python or relation among objects. Every object has a value, type, identity. Identity can be thought of as address of object in memory which never changes. The 'is' operator compares the identity of the objects. Type of an object is also unchangable.
    List of types available in python

    1. None -> Objects of type None have value None
    2. NotImplemented -> Same as None but it is returned when methods do not implement the operations on the operand
    3. Elipses -> Singled value accessed using ... or Ellipses. It contains int, bool, float(Real), complex
    4. Sequences -> We can use len() to find number of itemsin the sequencem, also support slicing. These are strings. Tuples, Bytes. ord() converts string to int, chr() converts char to int, str.encode() to convert str to bytes and bytes.decode() to convert bytes to str. Mutable sequences are Lists, Byte Arrays
    5. Set types -> They cannot be indexed by subscripts only iterated, unique with no repetitions. These are sets and frozen sets
    6. Mapping -> Dictionaries
    7. </ol>

      User definded functions

      • \__doc\__ -> The description of the object
      • \__name\__ -> It tells the function's name
      • \__qualname\__ -> It shows the path from a module's global scope to a class.
      • \__module\__ -> name of module function was defined in
      • \__code\__
      • \__globals\__ -> reference to the global variables of a function
      • \__dict\__ -> Namespace supporting arbitrary functions attributes
      • \__closure\__ -> None or tuple of cells containing binding of function's free variables
      • </ul>
        Instance methods

        • \__self\__ -> It is the class instance object
        • \__doc\__
        • \__name\__
        • \__module\__
        • \__bases\__ -> Tuples containing the base classes
        • </ul>
          Generator functions -> which uses yield. It return an iterator object which can be used to execute the body of the function.
          async def is called a coroutine function, which returns a coroutine object. It contains await, async with, async for.
          async def which uses yield is called asyncronous generator function, whose return object can be used in async for

          Class Instances A class instance has a namespace implemented as a dictionary where attribute references are first searched. When not found than instance's class has also a atrribute by that name. If not found than self is checked and if still not found than __getattr__() is checked. Attribute assignments and deletions always update the instance's dictionary.<\p>

          Various internal types used internally by the interpreter

          1. Code objects -> Represent byte-complied execuatble Python code or bytecode. They contain no references to mutable objects.
          2. Frame objects -> Represent execution frames, occur in traceback objects. Support one method called frame.clear() which clears all references to local variables.
          3. Traceback objects -> Represent a stack trace of an exception. It occurs when an exception occurs
          4. Slice objects -> Slices of \__getitem\__() methods or by slice().
          5. Static method objects -> Wrapper around any other object. When a static method object is retrieved from a class or a class instance, the object actually returns a wrapper object.
          6. Class method objects -> A wrapper aroudn another classes or class instances.

          Special method names

          1. object.\__new\__(cls[]) -> To create a new instance class cls
          2. object.\__init\__(self[]) -> A new instance when created by 1) then this it is called first before sending the control to the caller.
          3. object.\__del\__(self) -> To destroy the object
          4. object.\__repr\__(self) -> To compute the official string representation of an object.

          By default instances of classes have a dictioanry for attribute storage. This space consumption can become large when a large number of instances of a class are created. This default can be overridden by using __slots__ in a class definition. It reserves sapce for the declared vaariabels and prevents automatic creation of dict of each instance.
          If you want to dynamically declare new vairbales then add __dict__ to the sequence of strings in __slots__ declaration.

          Metaclasses
          The class creation process can be customized by passing the metaclass keyword argument in the class definition line or by inheriting from an existing class that included such ar argument.

          
          
          In [1]:
          class Meta(type):
              pass
          class MyClass(metaclass = Meta):
              pass
          class MySubclass(MyClass):
              pass
          

          Python programs are executed as code blocks which are module, function body, class definition. A code block is executed as a execution frame.

          To rasie an exception use raise or try --except. The finally clasue of such a staement can be used to specify cleanup code which does not handle the exception but is executed whether an exception occured or not in the preceding code.